part of internal;

class Actions {
   Buffer buf;
   State sta;
   Screen scr;

   // CONSTRUCTORS \\

   Actions (this.buf, this.sta, this.scr, DivElement cont) {
      if (buf == null) throw null_arg;
      if (sta == null) throw null_arg;
      if (scr == null) throw null_arg;

      // install handlers
      cont.attributes['tabIndex'] = '0';
      cont.onKeyDown.listen(keyDown);
      cont.onKeyPress.listen(keyPress);
      cont.onCut.listen(cut);
      cont.onCopy.listen(copy);
      cont.onPaste.listen(paste);
      cont.focus();
   }

   // METHODS \\

   // keyboard events
   void keyDown (KeyboardEvent ke) {
      int key = ke.keyCode;
      if (!ke.ctrlKey && !ke.altKey && !ke.shiftKey) {
         switch (key) {
            // newline insert/delete
            case ENTER : enter(); break;
            case BACKSPACE : backspace(); break;
            // cursor movemenet
            case UP : up(); break;
            case DOWN : down(); break;
            case LEFT : left(); break;
            case RIGHT : right(); break;
            case HOME : home(); break;
            case END : end(); break;
         }
      }
      if (badKeys.contains(key)) ke.preventDefault();
   }
   void keyPress (KeyboardEvent ke) {
      String char = ASCII.decode([ke.charCode]);
      buf.inssq(char, sta.head);
      sta.head.char++;
      scr.updln(sta.head.line - sta.start, buf.getln(sta.head.line));
      scr.setc(sta.head.char);
   }

   // newline handling
   void enter () {
      buf.insnl(sta.head);
      sta.head.line++;
      sta.head.char = 0;
      scr.updln(sta.head.line - 1, buf.getln(sta.head.line - 1));
      scr.insln(sta.head.line - sta.start, buf.getln(sta.head.line));
      scr.setl(sta.head.line - sta.start);
      scr.setc(sta.head.char);
   }
   void backspace () {
   }

   // cursor movement
   void up () {
      if (buf.isFirst(sta.head.line)) return;
      sta.head.line--;
      String temp = buf.getln(sta.head.line);
      if (temp.length >= sta.head.char);
      else sta.head.char = temp.length;
      if (sta.head.line + 1 == sta.start) {
         sta.start--;
         scr.sld(temp);
      }
      scr.setl(sta.head.line - sta.start);
      scr.setc(sta.head.char);
   }
   void down () {
      if (buf.isLast(sta.head.line)) return;
      sta.head.line++;
      String temp = buf.getln(sta.head.line);
      if (temp.length >= sta.head.char);
      else sta.head.char = temp.length;
      if (sta.head.line - 1 == sta.start + sta.max - 1) {
         sta.start++;
         scr.slu(temp);
      }
      scr.setl(sta.head.line - sta.start);
      scr.setc(sta.head.char);
   }
   void left () {
      if (sta.head.char == 0);
      else sta.head.char--;
      scr.setc(sta.head.char);
   }
   void right () {
      String temp = buf.getln(sta.head.line);
      if (temp.length == sta.head.char);
      else sta.head.char++;
      scr.setc(sta.head.char);
   }
   void home () {}
   void end () {}

   // cut copy paste
   void cut (Event e) {}
   void copy (Event e) {}
   void paste (Event e) {}
}
